package com.vshop.platform.system.crud.service;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.vshop.framework.core.constant.Const;
import com.vshop.framework.core.exception.ServiceException;
import com.vshop.framework.core.optionselect.OptionSelect;
import com.vshop.framework.core.optionselect.OptionSelectUtil;
import com.vshop.framework.core.util.MapstructUtil;
import com.vshop.framework.mybatisflex.page.PageData;
import com.vshop.framework.mybatisflex.page.PageQuery;
import com.vshop.platform.system.crud.dao.SysPostDao;
import com.vshop.platform.system.domain.entity.SysPost;
import com.vshop.platform.system.domain.param.SysPostParam;
import com.vshop.platform.system.domain.vo.SysPostVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Service
public class SysPostService {
    private final SysPostDao sysPostDao;

    public PageData<SysPostVo> page(SysPostParam param, PageQuery pageQuery) {
        QueryWrapper queryWrapper = sysPostDao.buildQueryWrapper(param);
        Page<SysPostVo> page = sysPostDao.pageAs(pageQuery.build(), queryWrapper, SysPostVo.class);
        return PageData.build(page);
    }

    public boolean save(SysPostParam param) {
        if (sysPostDao.checkExistPostName(param)) {
            throw new ServiceException("新增岗位'" + param.getPostName() + "'失败，岗位名称已存在");
        }
        if (sysPostDao.checkExistPostCode(param)) {
            throw new ServiceException("新增岗位'" + param.getPostCode() + "'失败，岗位编码已存在");
        }
        SysPost entity = MapstructUtil.convert(param, SysPost.class);
        return sysPostDao.save(entity);
    }

    public boolean update(SysPostParam param) {
        if (sysPostDao.checkExistPostName(param)) {
            throw new ServiceException("修改岗位'" + param.getPostName() + "'失败，岗位名称已存在");
        }
        if (sysPostDao.checkExistPostCode(param)) {
            throw new ServiceException("修改岗位'" + param.getPostCode() + "'失败，岗位编码已存在");
        }
        // 禁用状态
        if (ObjectUtil.equals(param.getStatus(), Const.StatusDisable)
                && sysPostDao.checkPostHasUserByPostId(param.getPostId())) {
            throw new ServiceException("该岗位包含已分配用户,不允许禁用");
        }
        SysPost entity = MapstructUtil.convert(param, SysPost.class);
        return sysPostDao.updateById(entity);
    }

    public boolean removeById(Long id) {
        Assert.notNull(id, "主键不能为空");

        if (sysPostDao.checkPostHasUserByPostId(id)) {
            SysPost entity = sysPostDao.getById(id);
            throw new ServiceException(String.format("%1$s 岗位已分配用户,不能删除!", entity.getPostName()));
        }
        return sysPostDao.removeById(id);
    }

    /**
     * 通过部门 ID查询岗位列表
     *
     * @param deptId 部门ID
     * @return 岗位选择列表信息
     */
    public List<OptionSelect<Long>> listOptionSelectByDeptId(Long deptId) {
        QueryWrapper queryWrapper = sysPostDao.buildQueryWrapperByDeptId(deptId);
        List<SysPostVo> list = sysPostDao.listAs(queryWrapper, SysPostVo.class);
        return OptionSelectUtil.build(list, (item, option) -> {
            option.setId(item.getPostId());
            option.setValue(item.getPostId());
            option.setLabel(item.getPostName());
        });
    }

}
